<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title th:replace="common/base::pagetitle"></title>
    <link th:replace="common/base::static"/>
    <link rel="stylesheet" th:href="@{/static/plugins/easyui/themes/material/easyui.css}">
    <link rel="stylesheet" th:href="@{/static/plugins/easyui/themes/icon.css}">
    <link rel="stylesheet" th:href="@{/static/css/wlb-treegrid.css}">
</head>
<body style="background-color: #f2f2f2;">
<div id="kPage" class="layui-fluid" style="padding: 0">
    <div class="layui-row">
        <!-- 主要内容块 -->
        <div class="layui-col-md12" id="kMainContentCol">
            <div class="layui-fluid wlb-fluid-padding15">
                <div class="layui-card">
                    <form>
                        <div class="layui-form layui-card-header wlb-card-header-auto">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <button class="layui-btn " lay-submit="" lay-filter="kTableSearchFilter">
                                        <i class="layui-icon layui-icon-search"></i>
                                    </button>
                                    <button class="layui-btn " type="reset">
                                        <i class="layui-icon layui-icon-refresh-1"></i>
                                    </button>
                                </div>
                            </div>
                        </div>
                    </form>

                    <div class="layui-card-body">
                        <table id="kContentTable" lay-filter="kContentTable"></table>
                    </div>
                </div>

            </div>
        </div>
    </div>
</div>

<script type="text/html" id="templateTypeTpl">
    {{# if(d.templateType === 0) { }}
    <span class="layui-badge layui-bg-green">系统模板</span>
    {{#  } else if(d.templateType === 1) { }}
    <span class="layui-badge layui-bg-orange">用户模板</span>
    {{# } }}
</script>

<!--表格行工具栏模板-->
<script type="text/html" id="rowToolbarTpl">
    <a class="layui-btn layui-btn-normal layui-btn-xs" data-event="edit" onclick="active.edit(this)" data-id="{{d.id}}"><i class="layui-icon layui-icon-edit"></i>编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger" data-event="del" onclick="active.del(this)" data-id="{{d.id}}"><i class="layui-icon layui-icon-delete" ></i>删除</a>
</script>

<!--头部工具栏模板-->
<script type="text/html" id="headToolbarTpl">
    <button class="layui-btn layui-btn-sm" lay-event="add">创建</button>
    <button class="layui-btn layui-btn-sm" lay-event="batchdel">删除</button>
</script>

<script th:replace="common/base::context"></script>
<script th:replace="common/base::mainjs"></script>
<script th:src="@{/static/plugins/easyui/easyloader.js}"></script>
<script type="text/javascript" th:inline="javascript">

    var active;
    layui.use(['element', 'form', 'layer', 'table', 'laytpl'], function () {
        var elemet = layui.element, form = layui.form, layer = layui.layer, table = layui.table;
        var tableId = 'kContentTable';

        // 实例化table
        var tableIns = table.render($.extend(config.layui.table, {
            elem: '#' + tableId
            , url: api.template.templateListData //数据接口
            ,response:{dataName: 'records',countName: 'total'}
            , toolbar: '#headToolbarTpl'
            , cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                , {field: 'name', title: '模板名称', width: 200, fixed: false}
                , {field: 'templateData', title: '模板数据', width: 200, fixed: false}
                , {field: 'description', title: '模板描述', width: 200, fixed: false}
                , {field: 'templateType', title: '模板类型', width: 100, templet: '#templateTypeTpl',fixed: false}
                , {field: 'templateBtype', title: '模板业务类型',  width: 130,align: 'center', templet: '#statusTpl',  fixed: false}
                , {field: '', title: '操作', width: 250, fixed: 'right', templet: '#rowToolbarTpl'}
            ]]
        }));
        var area = ['40%', '80%'];

        // 监听表格头部事件
        table.on('toolbar(' + tableId + ')', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var checkData = checkStatus.data; // 得到选中的数据
            switch (obj.event) {

                case 'add':
                    layer.open({
                        type: 2
                        , title: '创建模板'
                        , content: api.template.templateAddView
                        , maxmin: true
                        , area: area   // 宽高
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            //点击确认触发 iframe 内容中的按钮提交
                            var submit = layero.find('iframe').contents().find("#kFormSubmit");
                            submit.click();
                        }
                    });
                    break;

                case 'batchdel':
                    if (checkData.length === 0) {
                        return wlbKit.warningMsg('请选择数据');
                    }
                    layer.confirm('确定删除吗？', function (index) {

                        var ids = [];
                        $.each(checkData, function (i, n) {
                            ids[i] = n.id;
                        });

                        //执行 Ajax 后重载
                        $.ajax({
                            type: req.type.post,
                            url: api.template.templateDel,
                            data: {ids: ids.toString()},
                            success: function (r) {
                                if (r.code === req.status.ok) {
                                    table.reload(tableId);
                                    wlbKit.sucessMsg('已删除');
                                } else {
                                    wlbKit.errorMsg(r.msg);
                                }
                            }
                        });

                    });
                    break;
            }
        });


        // 监听事件
        active = {
            edit: function (that) {
                var id = $(that).data('id');
                layer.open({
                    type: 2
                    , title: '修改模板'
                    , content: api.template.templateEditView +"?id="+id
                    , maxmin: true
                    , area: area   // 宽高
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#kFormSubmit");
                        submit.click();
                    }
                });

            },

            del: function (that) {
                var id = $(that).data('id');
                layer.confirm('确定删除吗？', function (index) {
                    let ids = [id];
                    //执行 Ajax 后重载
                    $.ajax({
                        type: req.type.post,
                        url: api.template.templateDel,
                        data: {ids: ids.toString()},
                        async: false,
                        success: function (r) {
                            if (r.code === req.status.ok) {
                                table.reload(tableId);
                                wlbKit.sucessMsg('已删除');
                            } else {
                                wlbKit.errorMsg(r.msg);
                            }
                        }
                    });
                });

            },
        };

        wlbKit.activeInit(active);

        $(window).resize(function () {
            treeGridKit.fitRightFrozenCol(tableId);
        });

        // 列格式化函数
        var formatter = {
            templateTypeTpl: function (value, row, index) {
                return wlbKit.renderTpl('templateTypeTpl', row);
            },
            templateTypeTpl: function (value, row, index) {
                return wlbKit.renderTpl('templateTypeTpl', row);
            },
        };

        // 监听查询
        form.on('submit(kTableSearchFilter)', function (obj) {
            var params = obj.field;
            log('seach=', params); //

            tableIns.reload({
                where: params,
                page: {curr: 1}
            });
            return false;

        });

        // 绑定监听回车事件
        wlbKit.bindEnterEventForLayuiForm();

    });
</script>
</body>
</html>
